#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :demo_jd_chongzhi.py
# @Time      :2021/7/28 15:06
# @author    :Harry
'''
@case : 京东话费充值
@step：
1、访问京东首页；
2、首页快速菜单选择话费充值；
3、进行话费充值；
4、关闭浏览器；
'''
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.support.select import Select

driver = webdriver.Chrome()
driver.maximize_window()

driver.get("https://www.jd.com")
time.sleep(3)

# 话费充值
cz = driver.find_element_by_xpath('//div[@id="J_service"]/div[1]/ul/li[1]/a')

ac = ActionChains(driver)
ac.move_to_element(cz).perform()
time.sleep(2)

# 切换frame

# 外层frame
wframe = driver.find_element_by_xpath('//div[@id="J_service"]/div[2]/div[1]/iframe')
driver.switch_to.frame(wframe)
# 内层frame
huafei = driver.find_element_by_css_selector('iframe[src*="jdhome-cz-2017.htm"]')
driver.switch_to.frame(huafei)  # 进入frame

driver.find_element_by_id('phoneNumber').send_keys('18584817231')  # 输入电话号码
time.sleep(2)
# 下拉框选择面值
price = driver.find_element_by_id('select-price-money')
select = Select(price)
# select.select_by_index(2)
# select.select_by_value('50')
select.select_by_visible_text('500元')

time.sleep(3)
driver.find_element_by_class_name('btn-submit').click()  # 点击【快速充值】

# 退出frame
driver.switch_to.parent_frame()     # 退出上一层
driver.switch_to.default_content()  # 退出最外层

time.sleep(3)
driver.quit()